/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import pojo.SanPham;
import service.ChiTietDonDatHangService;
import service.SanPhamService;

/**
 *
 * @author MongKieu
 */
public class GioHangController extends AbstractController {

    private SanPhamService sanPhamService;
    private ChiTietDonDatHangService chiTietDonDatHangService;

    public ChiTietDonDatHangService getChiTietDonDatHangService() {
        return chiTietDonDatHangService;
    }

    public void setChiTietDonDatHangService(ChiTietDonDatHangService chiTietDonDatHangService) {
        this.chiTietDonDatHangService = chiTietDonDatHangService;
    }

    public SanPhamService getSanPhamService() {
        return sanPhamService;
    }

    public void setSanPhamService(SanPhamService sanPhamService) {
        this.sanPhamService = sanPhamService;
    }

    public GioHangController() {
    }

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mv = new ModelAndView("GioHang");
        try {
            HttpSession session = request.getSession();
            ArrayList<SanPham> gioHang = null;
            if (session.getAttribute("GioHang") == null) {
                gioHang = new ArrayList<SanPham>();
                session.setAttribute("GioHang", gioHang);
                session.setAttribute("Payment_Amount", "0");
            } else {
                gioHang = (ArrayList<SanPham>) session.getAttribute("GioHang");
            }
            // <editor-fold defaultstate="collapsed" desc="1. Them san pham">
            String action = request.getParameter("action");
            String a = request.getParameter("ISBN");
            if (action != null && action.equals("Them") && request.getParameter("ISBN") != null) {
                String ISBN = request.getParameter("ISBN");
                boolean kq = false;
                for (int i = 0; i < gioHang.size(); i++) {
                    SanPham sach = gioHang.get(i);
                    if (sach.getISBN().equals(ISBN) == true) {
                        sach.setSoLuongDangCo(sach.getSoLuongDangCo() + 1);
                        kq = true;
                        break;
                    }
                }
                if (kq == false) {
                    boolean reusultCheck = sanPhamService.kiemtraSanPhamTonTaiByISBN(ISBN);
                    if (reusultCheck == true) {
                        SanPham sanPham = new SanPham();
                        sanPham = sanPhamService.laySanPhamByISBN(ISBN);
                        sanPham.setSoLuongDangCo(1);
                        gioHang.add(sanPham);
                    }
                }
            }
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="2. Trả Lai sp">
            if (action != null && action.equals("TraLai") && request.getParameter("ISBN") != null) {
                String ISBN = request.getParameter("ISBN");
                //Tìm xem ms có trong danh sách đã mua
                for (int j = 0; j < gioHang.size(); j++) {
                    SanPham sanPham = gioHang.get(j);
                    if (sanPham.getISBN().equals(ISBN) == true) {
                        gioHang.remove(j);//Xóa sách j khỏi giỏ hàng
                        break;
                    }
                }
            }
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="3. Xóa Tất Cả">
            if (action != null && action.equals("XoaTatCa")) {
                session.removeAttribute("GioHang");
                gioHang.clear();
            }
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="4. Cập nhật">
            if (action != null && action.equals("CapNhat") && request.getParameter("ISBN") != null && request.getParameter("SoLuong") != null) {
                String ISBN = request.getParameter("ISBN");
                String soLuong = request.getParameter("SoLuong");
                for (int j = 0; j < gioHang.size(); j++) {
                    SanPham sanPham = gioHang.get(j);
                    if (sanPham.getISBN().equals(ISBN) == true) {
                        gioHang.get(j).setSoLuongDangCo(Integer.parseInt(soLuong));
                        break;
                    }
                }
            }
            // </editor-fold>

            // <editor-fold defaultstate="collapsed" desc="5.TongGiaTri">
            double tongGiaTri = 0;
            for (int i = 0; i < gioHang.size(); i++) {
                SanPham sp = gioHang.get(i);
                tongGiaTri = tongGiaTri + sp.getSoLuongDangCo() * sp.getGiaBanHienTai();
            }
            // </editor-fold>
            mv.addObject("listSanPham", gioHang);
            mv.addObject("TongTien", tongGiaTri);
        } catch (Exception ex) {
            System.out.println("Xay ra loi");
            mv = new ModelAndView("ThongBaoLoi");
            mv.addObject("messageError", "Xin chào bạn, thông tin giỏ hàng không hợp lệ.");
        }
        return mv;
    }
}
